class SearchController < ApplicationController
  caches_page :quicksearch, :view, :query

  def quicksearch
    @query = params[:query]
    if @query == nil || (@query.strip && @query.size == 0)
      render :nothing => true
      return
    end    

    @category = Category.find(params[:category]) if params[:category]
    @province = Province.find(params[:province]) if params[:province]
    @county = County.find(params[:county]) if params[:county]
    @ads = Ad.search(@query,params[:category], 20, params[:province], params[:county])
    
    @category_matches = Category.search(@query, 20, @province, @county)
    
    render :partial => "quicksearch"
  end

  def view
    @provinces = Province.find(:all, :order => "name")
    render :partial => "search"
  end

  def query
    @query = params[:query]
    Searchquery.query_issued(@query)
    @category = Category.find(params[:category]) if params[:category]
    @province = Province.find(params[:province]) if params[:province]
    @county = County.find(params[:county]) if params[:county]
    @ads = Ad.search(@query,params[:category], 9999, params[:province], params[:county])

    @page_title = "#{@query}"
    if @province
      @page_title += " i "
      if @county
        @page_title += @county.name + ", "
      end
      @page_title += @province.name
    end

    #render :text => "hej", :layout => layout
    render :partial => "result", :layout => layout
    #render :partial => "result"
  end
end
